package com.realtyManage.springboot.Service.impl;

import com.realtyManage.springboot.Dao.SysLogRepository;
import com.realtyManage.springboot.Dao.UserRepository;
import com.realtyManage.springboot.Model.Do.SysLog;
import com.realtyManage.springboot.Model.Do.User;
import com.realtyManage.springboot.Model.Vo.SysLogEntity;
import com.realtyManage.springboot.Model.Vo.UserEntity;
import com.realtyManage.springboot.Service.ICustomerService;
import com.realtyManage.springboot.Service.IUserService;
import com.realtyManage.springboot.Service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogRepository sysLogRepository;

    @Override
    public SysLog save(SysLog sysLog) {
        return sysLogRepository.saveAndFlush(sysLog);
    }

    @Override
    public Page<SysLog> findPagedByWhereClause(SysLogEntity sysLogEntity) {
        //分页查询
        Integer pageNumber = sysLogEntity.getPageNumber()-1;
        Integer pageSize = sysLogEntity.getPageSize();

        //排序条件
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(pageNumber,pageSize,sort);

        //动态查询条件
        Specification<SysLog> specification = (Specification<SysLog>)(root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            if (sysLogEntity.getOperation()!="") {
                // 根据模块名称查询
                Predicate p0 = criteriaBuilder.like(root.get("operation"),"%"+sysLogEntity.getOperation()+"%" );
                list.add(p0);
            }
            if (sysLogEntity.getsTime()!="" && sysLogEntity.getsTime() == "") {
                Predicate p1 = criteriaBuilder.like(root.get("time"),"%"+sysLogEntity.getsTime()+"%" );
                list.add(p1);
            }
            if (sysLogEntity.geteTime()!="" && sysLogEntity.geteTime() == "") {
                Predicate p2 = criteriaBuilder.like(root.get("time"),"%"+sysLogEntity.geteTime()+"%" );
                list.add(p2);
            }
            if (sysLogEntity.getsTime()!="" && sysLogEntity.geteTime()!="") {
                Predicate p3 = criteriaBuilder.between(root.get("time"),sysLogEntity.getsTime(),sysLogEntity.geteTime());
                list.add(p3);
            }
            return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };
        Page<SysLog> a = sysLogRepository.findAll(specification,pageable);
        System.out.println(a);
        return sysLogRepository.findAll(specification, pageable);
    }

   /* @Override
    public List<String> findByNameLike(String upName) {
        return sysLogRepository.findByNameLike(upName);
    }*/
}
